package pl.edu.agh.ssm.monitor.parsers.rtsp;

import pl.edu.agh.ssm.monitor.data.RTSPPacket;
import pl.edu.agh.ssm.monitor.data.RTSPPacketType;

public class RTSPParser {

	private int lineNr = 0;
	
	public RTSPPacket parse(byte[] data) throws InvalidRTSPDataException {
		RTSPPacket packet = new RTSPPacket();
		String strData = new String(data);
		
		String[] rtspLines = strData.split("\n");
		
		if(rtspLines.length == 0) {
			throw new InvalidRTSPDataException();
		}
//		System.out.println("Data to parse: ");
//		System.out.println(strData);
		parseFirstLine(rtspLines[0], packet);
		for(int i = 1; i < rtspLines.length; i++) {
//			System.out.println("Parsed line: " + rtspLines[i]);
			parseNextLine(rtspLines[i], packet, i);
		}
		
		StringBuilder builder = new StringBuilder();
		for(int i = lineNr + 1; i < rtspLines.length; i++) {
			builder.append(rtspLines[i]);
			builder.append("\n");
		}
		packet.setPacketContent(builder.toString());
		
		packet.setFullPacket(strData);
//		System.out.println(new String(data));
		return packet;
	}
	
	private void parseFirstLine(String rtspLine, RTSPPacket packet) throws InvalidRTSPDataException {
//		System.out.println(rtspLine);
		String[] data = rtspLine.split(" ");
//		System.out.println("Data length: " + data.length);
		if(data.length != 3) {
			throw new InvalidRTSPDataException();
		}
		try {
			packet.setType(RTSPPacketType.valueOf(data[0]));
		} catch(IllegalArgumentException ex) {
			throw new InvalidRTSPDataException();
		}
//		System.out.println("First line parsed");
		packet.setTypeAttr(data[1]);
		packet.setRtspVersion(data[2]);
//		System.out.println("All data set");
		
	}
	
	private void parseNextLine(String rtspLine, RTSPPacket packet, int lineNr) throws InvalidRTSPDataException {
		
		/* General attributes */
		if(rtspLine.startsWith("CSeq")) {
			String[] s = rtspLine.trim().split(":", 2);
			packet.setCSeq(Long.valueOf(s[1].trim()).longValue());
			this.lineNr = lineNr;
			return;
		}
		if(rtspLine.startsWith("Session")) {
			String[] s = rtspLine.trim().split(":", 2);
			packet.setSessionID(Long.valueOf(s[1].trim()).longValue());
			this.lineNr = lineNr;
			return;
		}
//		if(s[0].equalsIgnoreCase("user-agent")) {
//			packet.setUserAgent(s[1].trim());
//			return;
//		}
//		/* DESCRIBE specific*/
//		if(s[0].equalsIgnoreCase("accept")) {
//			packet.setAccept(s[1].trim());
//			return;
//		}
//		if(s[0].equalsIgnoreCase("bandwidth")) {
//			packet.setBandwidth(s[1].trim());
//			return;
//		}
//		
//		/* SETUP specific fields */
//		if(s[0].equalsIgnoreCase("transport")) {
//			String[] tmp = s[1].trim().split(";");
//			if(tmp.length != 3) {
////				System.out.println("Actual SETUP tmp length: " + tmp.length);
//				throw new InvalidRTSPDataException();
//			}
//			packet.setTransportProtocol(tmp[0].trim());
//			packet.setTransportType(tmp[1].trim());
//			String[] numb = tmp[2].substring(tmp[2].lastIndexOf('=') + 1).split("-");
//			packet.setClientRTPPort(Integer.valueOf(numb[0]).intValue());
//			packet.setClientRTCPPort(Integer.valueOf(numb[1]).intValue());
//			return;
//		}
//		
//		/* PLAY specific fields */
//		if(s[0].equalsIgnoreCase("range")) {
//			String[] tmp = s[1].split("=");
//			if(tmp.length != 2) {
//				throw new InvalidRTSPDataException();
//			}
//			packet.setTimeType(tmp[0]);
//			packet.setTimeRange(tmp[1]);
//			return;
//		}
	}
}
